/**
 * Company : SIANGKLOM.
 * Created By : Chatree Siangklom
 * Created On : Sep 30, 2013  9:43:40 AM 
 * e-Mail : dr_chatree@hotmail.com
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : com.tis.interfaces.controller.queue
 * File Name : QueueWaitController.java
 * TODO
 */
package com.tis.interfaces.controller.queue;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import promis.utility.CommonService;

import com.tis.database.entitys.queue.SyncQueue;
import com.tis.database.entitys.staging.ApplicationLoging;
import com.tis.database.services.queue.ISyncQueueServices;
import com.tis.interfaces.controller.commons.CommonController;

/**
 * @author Chatree Siangklom of Administrator
 * Date =Sep 30, 2013 : 9:43:40 AM 
 * CreateBy=Chatree Siangklom
 * TODO
 */
@Controller
@RequestMapping(QueueWaitController.url)
public class QueueWaitController extends CommonController {
	private static final Logger logging = LoggerFactory.getLogger(QueueWaitController.class);

	public static final String url = "/queue/pending";
	private static final String uri = "/queue";
	
	@RequestMapping(value="list/index", method = RequestMethod.GET)
	public String Backlogs(HttpServletRequest request) throws Exception {
		setCurrentAppsInstanceNo();
		KeepLogs(new ApplicationLoging(getCurrentAppsInstanceNo(), "W", "WELCOME", null, getCurrentAppsInstanceNo()));
		
		//Put History 		
		try { UserAccessTime(request, this.toString()); }catch(Exception e) {e.printStackTrace();}
		
		try { if(!UserLoginValidation((request))) return "redirect:/login";  } catch (Exception e) {e.getStackTrace(); logging.error(e.getMessage()); throw e; }

		logging.info("uri {queue/pending} = " + (uri + "/pending"));
		
		return uri + "/processedpending‎";
	}
	
	@Autowired
	private ISyncQueueServices iSyncQueueServices;
	
	@RequestMapping(value="list/search/json", method = RequestMethod.GET, headers="accept=application/json")
	@ResponseBody
	public List<SyncQueue> BacklogsSearch(
											@RequestParam(value="queueId", required=false) String queueId,
											@RequestParam(value="processModule", required=false) String processModule,
											@RequestParam(value="processingName", required=false) String processingName,
											@RequestParam(value="processLevel", required=false) String processLevel,
											@RequestParam(value="procCreatedonStart", required=false) String procCreatedonStart,
											@RequestParam(value="procCreatedonOnEnd", required=false) String procCreatedonOnEnd,
											HttpServletRequest request, 
											HttpServletResponse response) throws Exception {
		try {
			setCurrentAppsInstanceNo();
			KeepLogs(new ApplicationLoging(getCurrentAppsInstanceNo(), "W", "WELCOME", null, getCurrentAppsInstanceNo()));
			
			if(!UserLoginValidation((request))) { response.setStatus(500); return null;}
			
			logging.info("queueId" + queueId);
			logging.info("processModule" + processModule);
			logging.info("processingName" + processingName);
			logging.info("processLevel" + processLevel);
			logging.info("procCreatedonStart" + procCreatedonStart);
			logging.info("procCreatedonOnEnd" + procCreatedonOnEnd);
			
			List<SyncQueue> lst = iSyncQueueServices.search(queueId, processModule, processingName, processLevel, procCreatedonStart, procCreatedonOnEnd);
			logging.info("SyncQueue size =" + CommonService.getSize(lst));
			if(CommonService.getSize(lst) > 0)		
				return lst;
			else 
				return null;
			
		}catch(Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

}
